package com.yahoo.platform.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;

public class DirHelper {

	 
	private static void mkdir(File file){
		if(file.getParentFile().exists()){
			file.mkdir();
		}else {
			mkdir(file.getParentFile());
			file.mkdir();
		}
	}
	
	public static void checkDir(String inputFileName){
		File file = new File(inputFileName);
		if(!file.exists())
			mkdir(file);
		
		return;
	}
	
	public static List<File>  listFiles(String srcFolder, String ext){
		File folder = new File(srcFolder);
		File[] files = folder.listFiles();
		List<File> extFiles = new CopyOnWriteArrayList<File>();
		for(File f:files){
			String fileName = f.getName();
			if(fileName.toLowerCase().endsWith(ext))
				extFiles.add(f);
		}
		return extFiles;
	}
	
	public static void copyFolder(File srcFile, String dstFolder, String excludeFile){
		String[] exts =  excludeFile.split(",");
		File[] files = srcFile.listFiles();
		for(File subFile : files){
			boolean hasExt = false;
			if(subFile.isDirectory())
				continue;
			else {
				String fileName = subFile.getName();
				for(String ext : exts){
					if(fileName.toLowerCase().endsWith(ext)){
						hasExt = true;
						break;
					}
				}
				if(!hasExt){
					String dstFile = dstFolder + "\\" + subFile.getName();
					doCopy(subFile.getAbsolutePath(), dstFile);
				}
			}
		}
	}
	
	public static void doCopy(String srcFile, String dstFile){
		InputStream in = null;
		OutputStream out = null;
		
		File file = new File(srcFile);
		if(!file.exists())
			return;
		
		try {
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(dstFile);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		byte[] buffer = new byte[1024];
		try {
			while(in.read(buffer) != -1){
				out.write(buffer);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(out != null)
					out.close();
				if(in != null)
					in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void deleFile(String fileName){
		File file = new File(fileName);
		if(file.exists() && !file.isDirectory())
			file.delete();
	}

	public static void deleFolder(String folderName){
		File folder = new File(folderName);
		if(!folder.exists() || !folder.isDirectory())
			return;
		Stack<File> listFiles = new Stack<File>(); 
		Queue<File> listFolders = new LinkedBlockingDeque<>(); 
		listFolders.add(folder);
		listFiles.push(folder);
		do {
			folder = listFolders.remove();
			File[] files = folder.listFiles();
			for (File file : files) {
				if(file.isDirectory())
				{
					listFiles.push(file);
					listFolders.add(file);
				}
				else {
					DirHelper.deleFile(file.getAbsolutePath());
				}
			}
		} while (!listFolders.isEmpty());
		
		while (!listFiles.isEmpty()) {
			folder = listFiles.pop();
			folder.delete();
		}
	}
}
